package com.example.game_dafangdaxie.algorithm;

import com.example.game_dafangdaxie.StringConstant.Point;
import com.example.game_dafangdaxie.StringConstant.TouchList;
import com.example.game_dafangdaxie.StringConstant.TouchXY;

import java.util.ArrayList;

/***
 * 是否达成条件判断
 * 落子后，判断形状
 * 达成条件
 * 提示并加相应步数
 * 优先判断龙>5>4=3=方
 * 仅达成最高的奖励，一次下子，奖励不叠加
 *  create by LiuTong
 *   2021.9.27
 */
public class ConditionalJudgment {

    private Point CurrentPoint;
    private int play;
    private ArrayList<TouchXY> points;
    private int pre;//预置 预下棋没有填入该棋子 1

    /***
     *
     * @param CurrentPoint 当前落子位置
     * @param points 棋盘数组
     * @param play 选手 0：红 1：蓝
     * @param pre 模拟下棋时为1，其他情况为0
     */

    public ConditionalJudgment(Point CurrentPoint,ArrayList<TouchXY> points,int play,int pre){

        this.CurrentPoint=CurrentPoint;
        this.points=points;
        this.play=play;
        this.points=points;
        this.pre=pre;
    }

    public boolean isGameLong(){
        boolean H=false;
        boolean S=false;
        boolean YX=false;
        boolean ZX=false;
        //横向
        ArrayList<TouchXY> HTouchXy=new ArrayList<>();
        for(int i=0;i<6;i++){
            /***
             * 满足点
             */
           Point point=new Point(CurrentPoint.x,i);
           HTouchXy.add(new TouchXY(point,play));
        }
        int Hcounts=0;
        for(TouchXY touchXY:HTouchXy){
            if(TouchList.Contains(points,touchXY)){
                Hcounts++;
            }
        }
        if(Hcounts==(6-pre)){
            H=true;
        }
        //纵向
        ArrayList<TouchXY> STouchXy=new ArrayList<>();
        for(int i=0;i<6;i++){
            Point point=new Point(i,CurrentPoint.y);
            STouchXy.add(new TouchXY(point,play));
        }
        int SCounts=0;
        for(TouchXY touchXY:STouchXy){
            if(TouchList.Contains(points,touchXY)){
                SCounts++;
            }
        }
        if(SCounts==(6-pre)){
            S=true;
        }
        //右斜x==y
        ArrayList<TouchXY> YXTouchXy=new ArrayList<>();
        int YXCounts=0;
        if(CurrentPoint.x==CurrentPoint.y){
            for(int i=0;i<6;i++){
                Point point=new Point(i,i);
                YXTouchXy.add(new TouchXY(point,play));
            }
            for(TouchXY touchXY:YXTouchXy){
                if(TouchList.Contains(points,touchXY)){
                    YXCounts++;
                }
            }
            if(YXCounts==(6-pre)){
                YX=true;
            }
        }else {
            YX=false;
        }
        //左斜x+y==5
        ArrayList<TouchXY> ZXTouchXy=new ArrayList<>();
        if(CurrentPoint.x+CurrentPoint.y==5){
            int ZXCounts=0;
            for (int i = 0; i < 6; i++) {
                Point point = new Point(i, 5 - i);
                ZXTouchXy.add(new TouchXY(point, play));
            }
            for (TouchXY touchXY : ZXTouchXy) {
                if (TouchList.Contains(points, touchXY)) {
                    ZXCounts++;
                }
            }
            if (ZXCounts == (6-pre)) {
                ZX = true;
            }
        }else {
            ZX=false;
        }

       return S||H||ZX||YX;
    }

    public boolean isGameFive(){
        ArrayList<TouchXY> tempTouchXY;
        boolean f=false;
        int Counts=0;
        if(CurrentPoint.x+CurrentPoint.y==4){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=0;i<5;i++){
                tempTouchXY.add(new TouchXY(new Point(i,4-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(5-pre)){
                f= true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==-1){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=0;i<5;i++){
                tempTouchXY.add(new TouchXY(new Point(i,i+1),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(5-pre)){
                f= true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==1){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=0;i<5;i++){
                tempTouchXY.add(new TouchXY(new Point(i+1,i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(5-pre)){
                f= true;
            }
        }
        if(CurrentPoint.x+CurrentPoint.y==6){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=1;i<=5;i++){
                tempTouchXY.add(new TouchXY(new Point(i,6-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(5-pre)){
                f= true;
            }
        }
        return f;

    }

    public boolean isGameFour(){
        ArrayList<TouchXY> tempTouchXY;
        int Counts=0;
        if(CurrentPoint.x+CurrentPoint.y==3){
            tempTouchXY=new ArrayList<>();
            for(int i=0;i<4;i++){
                tempTouchXY.add(new TouchXY(new Point(i,3-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(4-pre)){
                Counts=0;
                return true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==-2){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=0;i<4;i++){
                tempTouchXY.add(new TouchXY(new Point(i,i+2),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(4-pre)){
                Counts=0;
                return true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==2){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=0;i<4;i++){
                tempTouchXY.add(new TouchXY(new Point(i+2,i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(4-pre)){
                Counts=0;
                return true;
            }
        }
        if(CurrentPoint.x+CurrentPoint.y==7){
            tempTouchXY=new ArrayList<>();
            Counts=0;
            for(int i=2;i<=5;i++){
                tempTouchXY.add(new TouchXY(new Point(i,7-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(4-pre)){
                Counts=0;
                return true;
            }
        }
        return false;
    }

    public boolean isThree(){
        ArrayList<TouchXY> tempTouchXY;
        boolean f=false;
        int Counts=0;
        if(CurrentPoint.x+CurrentPoint.y==2){
            tempTouchXY=new ArrayList<>();
            for(int i=0;i<3;i++){
                tempTouchXY.add(new TouchXY(new Point(i,2-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(3-pre)){
                f=true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==-3){
            Counts=0;
            tempTouchXY=new ArrayList<>();
            for(int i=0;i<3;i++){
                tempTouchXY.add(new TouchXY(new Point(i,i+3),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(3-pre)){
                f=true;
            }
        }
        if(CurrentPoint.x-CurrentPoint.y==3){
            Counts=0;
            tempTouchXY=new ArrayList<>();
            for(int i=0;i<3;i++){
                tempTouchXY.add(new TouchXY(new Point(i+3,i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(3-pre)){
                f=true;
            }
        }
        if(CurrentPoint.x+CurrentPoint.y==8){
            Counts=0;
            tempTouchXY=new ArrayList<>();
            for(int i=3;i<=5;i++){
                tempTouchXY.add(new TouchXY(new Point(i,8-i),play));
            }
            for(TouchXY touchXY:tempTouchXY){
                if(TouchList.Contains(points,touchXY)){
                    Counts++;
                }
            }
            if(Counts==(3-pre)){
                f=true;
            }
        }
        return f;
    }

    public  boolean isFang(){
        boolean f=false;
        ArrayList<TouchXY> TouchXyList;
        Point point;
        int counts;
        //左上 ok
        TouchXyList=new ArrayList<>();
        counts=0;
        point=new Point(CurrentPoint.x,CurrentPoint.y);
        TouchXyList.add(new TouchXY(point,play));
        if(CurrentPoint.x-1>=0 && CurrentPoint.y-1>=0){
            point=new Point(CurrentPoint.x-1,CurrentPoint.y);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x-1,CurrentPoint.y-1);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x,CurrentPoint.y-1);
            TouchXyList.add(new TouchXY(point,play));
        }

        for(TouchXY touchXY:TouchXyList){
            if(TouchList.Contains(points,touchXY)){
                counts++;
            }
        }
        if(counts==(4-pre)){
            f= true;
        }


        //右上ok
        TouchXyList=new ArrayList<>();
        counts=0;
         point=new Point(CurrentPoint.x,CurrentPoint.y);
        TouchXyList.add(new TouchXY(point,play));
        if(CurrentPoint.x+1<=5&&CurrentPoint.y-1>=0){
            point=new Point(CurrentPoint.x+1,CurrentPoint.y);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x+1,CurrentPoint.y-1);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x,CurrentPoint.y-1);
            TouchXyList.add(new TouchXY(point,play));
        }

        for(TouchXY touchXY:TouchXyList){
            if(TouchList.Contains(points,touchXY)){
                counts++;
            }
        }
        if(counts==(4-pre)){
            f= true;
        }

        //左下 ok
        TouchXyList=new ArrayList<>();
        counts=0;
        point=new Point(CurrentPoint.x,CurrentPoint.y);
        TouchXyList.add(new TouchXY(point,play));
        if(CurrentPoint.x-1>=0&&CurrentPoint.y+1<=5){
            point=new Point(CurrentPoint.x-1,CurrentPoint.y);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x-1,CurrentPoint.y+1);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x,CurrentPoint.y+1);
            TouchXyList.add(new TouchXY(point,play));
        }
        for(TouchXY touchXY:TouchXyList){
            if(TouchList.Contains(points,touchXY)){
                counts++;
            }
        }
        if(counts==(4-pre)){
            f= true;
        }

        //右下 ok
        TouchXyList=new ArrayList<>();
        counts=0;
        point=new Point(CurrentPoint.x,CurrentPoint.y);
        TouchXyList.add(new TouchXY(point,play));
        if(CurrentPoint.x+1<=5&&CurrentPoint.y+1<=5){
            point=new Point(CurrentPoint.x+1,CurrentPoint.y);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x+1,CurrentPoint.y+1);
            TouchXyList.add(new TouchXY(point,play));
            point=new Point(CurrentPoint.x,CurrentPoint.y+1);
            TouchXyList.add(new TouchXY(point,play));
        }

        for(TouchXY touchXY:TouchXyList){
            if(TouchList.Contains(points,touchXY)){
                counts++;
            }
        }
        if(counts==(4-pre)){
            f= true;
        }
        return f;

    }

    public int getSteps(){
        int steps=0;
        if(isGameLong()) {
            steps = 3;
        }else if(isGameFive()){
            steps=2;
        } else if(isGameFour()||isFang()||isThree()) {
            steps=1;
       }
        return steps;
    }
}
